from django.db import models
from django.contrib.auth.models import AbstractUser, Group
from django.contrib.auth.models import Permission
# from django.contrib.contenttypes.models import ContentType

# 添加角色权限
# view_user_group_permission = None
# add_user_group_permission = None
# change_user_group_permission = None
# delete_user_group_permission = None
# content_type = ContentType.objects.get_for_model(Group)
# try:
#     view_user_group_permission = Permission.objects.get(
#         codename='view_user_group'
#     )
# except Permission.DoesNotExist:
#     view_user_group_permission = Permission.objects.create(
#         codename='view_user_group',
#         name='查看用户组',
#         content_type=content_type,
#     )
# try:
#     add_user_group_permission = Permission.objects.get(
#         codename='add_user_group'
#     )
# except Permission.DoesNotExist:
#     add_user_group_permission = Permission.objects.create(
#         codename='add_user_group',
#         name='添加用户组',
#         content_type=content_type,
#     )
# try:
#     change_user_group_permission = Permission.objects.get(
#         codename='change_user_group'
#     )
# except Permission.DoesNotExist:
#     change_user_group_permission = Permission.objects.create(
#         codename='change_user_group',
#         name='修改用户组',
#         content_type=content_type,
#     )
# try:
#     delete_user_group_permission = Permission.objects.get(
#         codename='delete_user_group'
#     )
# except Permission.DoesNotExist:
#     delete_user_group_permission = Permission.objects.create(
#         codename='delete_user_group',
#         name='删除用户组',
#         content_type=content_type,
#     )


class User(AbstractUser):
    # 0表示男，1表示女
    gender = models.CharField(
        max_length=10, verbose_name='性别', choices=(('0', u'男'), ('1', u'女')))

    class Meta:
        db_table = 'user'
        # 权限设置
        default_permissions = ()
        permissions = (
            ('view_user', u'查看用户'),
            ('add_user', u'添加用户'),
            ('delete_user', u'删除用户'),
            ('change_user', u'修改用户'),
        )
